<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/23/023
 * Time: 21:12
 */

namespace app\queue;

use app\model\AdminQueueMessage;
use think\Queue;

class QueuePub
{
    /**
     * @param $delay int 延时的时长
     * @param $jobType int 任务类型，为QueueTypes中定义过的常量
     * @param $data array 数据
     * @param $queueType int 队列类型，为QueueTypes中定义过的常量，默认可不填
     * @param int $level 等级
     * @return mixed
     */
    public static function later($delay, $jobType, $data, $queueType = null, $level = 6)
    {
        $job = QueueTypes::getQueueClass($jobType);
        $queue = QueueTypes::getQueue($queueType == null ? $jobType : $queueType);
        $done = AdminQueueMessage::create(['level' => $level, 'content' => json_encode($data), 'type' => $jobType]);
        if ($done) {
            $data['queue_id'] = $done->id;
            return Queue::later($delay, $job, $data, $queue);
        }
        return false;
    }
    /**
     * @param $jobType int 任务类型，为QueueTypes中定义过的常量
     * @param $data array 数据
     * @param $queueType int 队列类型，为QueueTypes中定义过的常量，默认可不填
     * @param int $level 等级
     * @return mixed
     */
    public static function push($jobType, $data, $queueType = null, $level = 6)
    {
        $job = QueueTypes::getQueueClass($jobType);
        $queue = QueueTypes::getQueue($queueType == null ? $job : $queueType);
        $done = AdminQueueMessage::create(['level' => $level, 'content' => json_encode($data), 'type' => $job]);
        if ($done) {
            $data['queue_id'] = $done->id;
            return Queue::push($job, $data, $queue);
        }
        return false;

    }
}